package com.ljc.exception;

import com.ljc.entity.BaseResponse;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse<String> commonExceptionHandler(Exception e) {
        String message = e.getMessage();
        e.printStackTrace();
        if (e instanceof ConditionException) {
            String code = ((ConditionException) e).getCode();
            return new BaseResponse<>(code, message);
        } else {
            return new BaseResponse<>("500", message);
        }
    }
}
